package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.vo.WxHomeIndexFloorGoodsVo;
import com.cskaoyan.market.vo.WxHomeIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MYM
 * @Date: 2024/05/21/11:31
 * @Description:
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public WxHomeIndexVo getHome() {
        //banner
        List<MarketAd> marketAds = marketAdMapper.selectByExample(null);
        int bannerEndIndex = Math.min(marketAds.size(), 6);
        List<MarketAd> bannerList = marketAds.subList(0, bannerEndIndex);
        //brandList
        List<MarketBrand> brand = marketBrandMapper.selectByExample(null);
        for (MarketBrand marketBrand : brand) {
            marketBrand.setSortOrder(null);
            marketBrand.setAddTime(null);
            marketBrand.setUpdateTime(null);
            marketBrand.setDeleted(null);
        }
        int brandEndIndex = Math.min(brand.size(), 10);
        List<MarketBrand> brandList = brand.subList(0, brandEndIndex);
        //channel
        MarketCategoryExample channelExample = new MarketCategoryExample();
        channelExample.createCriteria().andPidEqualTo(0);
        List<MarketCategory> channelList = marketCategoryMapper.selectByExample(channelExample);
        for (MarketCategory marketCategory : channelList) {
            marketCategory.setAddTime(null);
            marketCategory.setDeleted(null);
            marketCategory.setAddTime(null);
            marketCategory.setSortOrder(null);
            marketCategory.setUpdateTime(null);
            marketCategory.setPid(null);
            marketCategory.setKeywords(null);
            marketCategory.setPicUrl(null);
            marketCategory.setLevel(null);
        }
        int channelEndIndex = Math.min(channelList.size(), 10);
        List<MarketCategory> channel = channelList.subList(0, channelEndIndex);
        //couponList
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.createCriteria().andTypeEqualTo((short) 0);
        List<MarketCoupon> coupon = marketCouponMapper.selectByExample(couponExample);
        for (MarketCoupon marketCoupon : coupon) {
            marketCoupon.setAddTime(null);
            marketCoupon.setDeleted(null);
            marketCoupon.setGoodsValue(null);
            marketCoupon.setLimit(null);
            marketCoupon.setTotal(null);
            marketCoupon.setType(null);
            marketCoupon.setStatus(null);
            marketCoupon.setGoodsType(null);
            marketCoupon.setGoodsValue(null);
            marketCoupon.setCode(null);
            marketCoupon.setTimeType(null);
            marketCoupon.setStartTime(null);
            marketCoupon.setUpdateTime(null);
        }
        int couponEndIndex = Math.min(coupon.size(), 3);
        List<MarketCoupon> couponList = coupon.subList(0, couponEndIndex);
        //floorGoods
        List<WxHomeIndexFloorGoodsVo> floorGoodsList = new ArrayList<>();
        MarketCategoryExample floorCategoryExample = new MarketCategoryExample();
        floorCategoryExample.createCriteria().andPidEqualTo(0);
        List<MarketCategory> floorCategoryList = marketCategoryMapper.selectByExample(floorCategoryExample);
        int floorCategoryEndIndex = Math.min(floorCategoryList.size(), 6);
        List<MarketCategory> floorCategory = floorCategoryList.subList(0, floorCategoryEndIndex);
        for (MarketCategory marketCategory : floorCategory) {
            Integer categoryId = marketCategory.getId();
            String categoryName = marketCategory.getName();
            MarketCategoryExample floorGoodsCategoryExample = new MarketCategoryExample();
            floorGoodsCategoryExample.createCriteria().andPidEqualTo(categoryId);
            List<MarketCategory> floorGoodsCategoryList = marketCategoryMapper.selectByExample(floorGoodsCategoryExample);
            for (MarketCategory floorGoodsCategory : floorGoodsCategoryList) {
                MarketGoodsExample floorGoodsExample = new MarketGoodsExample();
                floorGoodsExample.createCriteria().andCategoryIdEqualTo(floorGoodsCategory.getId());
                List<MarketGoods> floorGoodList = marketGoodsMapper.selectByExample(floorGoodsExample);
                for (MarketGoods floorGood : floorGoodList) {
                    floorGood.setGoodsSn(null);
                    floorGood.setCategoryId(null);
                    floorGood.setBrandId(null);
                    floorGood.setGallery(null);
                    floorGood.setKeywords(null);
                    floorGood.setIsOnSale(null);
                    floorGood.setSortOrder(null);
                    floorGood.setShareUrl(null);
                    floorGood.setUnit(null);
                    floorGood.setDetail(null);
                    floorGood.setAddTime(null);
                    floorGood.setUpdateTime(null);
                    floorGood.setDeleted(null);
                }
                int floorGoodsEndIndex = Math.min(floorGoodList.size(), 7);
                List<MarketGoods> goodsList = floorGoodList.subList(0, floorGoodsEndIndex);
                floorGoodsList.add(new WxHomeIndexFloorGoodsVo(goodsList, categoryId, categoryName));
            }

        }
        //hotGoodsList
        MarketGoodsExample hotGoodsExample = new MarketGoodsExample();
        hotGoodsExample.createCriteria().andIsHotEqualTo(true);
        List<MarketGoods> hotGoods = marketGoodsMapper.selectByExample(hotGoodsExample);
        for (MarketGoods hotGood : hotGoods) {
            hotGood.setGoodsSn(null);
            hotGood.setCategoryId(null);
            hotGood.setBrandId(null);
            hotGood.setGallery(null);
            hotGood.setKeywords(null);
            hotGood.setIsOnSale(null);
            hotGood.setSortOrder(null);
            hotGood.setShareUrl(null);
            hotGood.setUnit(null);
            hotGood.setDetail(null);
            hotGood.setAddTime(null);
            hotGood.setUpdateTime(null);
            hotGood.setDeleted(null);
        }
        int hotGoodsEndIndex = Math.min(hotGoods.size(), 5);
        List<MarketGoods> hotGoodsList = hotGoods.subList(0, hotGoodsEndIndex);
        //newGoodsList
        MarketGoodsExample newGoodsExample = new MarketGoodsExample();
        newGoodsExample.createCriteria().andIsNewEqualTo(true);
        List<MarketGoods> newGoods = marketGoodsMapper.selectByExample(newGoodsExample);
        for (MarketGoods newGood : newGoods) {
            newGood.setGoodsSn(null);
            newGood.setCategoryId(null);
            newGood.setBrandId(null);
            newGood.setGallery(null);
            newGood.setKeywords(null);
            newGood.setIsOnSale(null);
            newGood.setSortOrder(null);
            newGood.setShareUrl(null);
            newGood.setUnit(null);
            newGood.setDetail(null);
            newGood.setAddTime(null);
            newGood.setUpdateTime(null);
            newGood.setDeleted(null);
        }
        int newGoodsEndIndex = Math.min(newGoods.size(), 5);
        List<MarketGoods> newGoodsList = newGoods.subList(0, newGoodsEndIndex);
        //topicList
        List<MarketTopic> topic = marketTopicMapper.selectByExample(null);
        for (MarketTopic marketTopic : topic) {
            marketTopic.setContent(null);
            marketTopic.setSortOrder(null);
            marketTopic.setGoods(null);
            marketTopic.setAddTime(null);
            marketTopic.setUpdateTime(null);
            marketTopic.setDeleted(null);
        }
        int topicEndIndex = Math.min(topic.size(), 5);
        List<MarketTopic> topicList = topic.subList(0, topicEndIndex);
        return new WxHomeIndexVo(bannerList, brandList, channel, couponList, floorGoodsList, hotGoodsList, newGoodsList, topicList);
    }
}
